package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketAdmin;
import com.cskaoyan.bean.po.MarketAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.chaosbean.system.vo.SystemListVo;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.service.adminservice.AdminService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/09/06 11:44
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper adminMapper;

    @Override
    public MarketAdmin queryById(Integer id) {
        MarketAdmin marketAdmin = adminMapper.selectByPrimaryKey(id);
        return marketAdmin;
    }

    @Override
    public void modify(MarketAdmin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
    }

    @Override
    public List<MarketAdmin> query(String username, String password, String[] avatars,
                                   Integer page, Integer limit) {
        // 执行查询之前 → 使用PageHelper开启分页就可以了
        PageHelper.startPage(page, limit);
        MarketAdminExample example = new MarketAdminExample();
        example.setDistinct(true);
        //example.setOrderByClause("username asc");
        // List<Criterion> 向这个列表里塞入数据
        // 条件查询

        MarketAdminExample.Criteria criteria = example.createCriteria();//这个实例里封装了Criterion的列表
        // 使用Example中的Criteria提供的方法，就可以构造Criterion实例，并且呢添加到list中
        /*criteria.andUsernameEqualTo(username)
                .andPasswordLike("%" + password + "%")
                .andAvatarIn(Arrays.asList(avatars));*/
        if (username != null && !"".equals(username)) {//StringUtils isEmpty
            criteria.andUsernameEqualTo(username);
        }
        if (password != null && !"".equals(password)) {

            criteria.andPasswordLike("%" + password + "%");
        }
        if (avatars != null && avatars.length > 0) {

            criteria.andAvatarIn(Arrays.asList(avatars));
        }
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(example);
        // page limit total
        // 把mybatis查询出来的原始结果放进去
        PageInfo pageInfo = new PageInfo(marketAdmins);
        long total = pageInfo.getTotal();
        return marketAdmins;
    }

    @Override
    /**
     * /list?page=1&limit=20&sort=add_time&order=desc
     * basePageInfo.getPage
     * basePageInfo.getLimit
     * basePageInfo.getSort
     * basePageInfo.getOrder
     */
    public SystemListVo listAdmin(BasePageInfo basePageInfo, String username) {
        //开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        SystemListVo listVo = new SystemListVo();
        MarketAdminExample example = new MarketAdminExample();
        //排序
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketAdminExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> listBean = adminMapper.selectByExample(example);

        listVo.setList(listBean);
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(listBean);
        listVo.setLimit(pageInfo.getPageSize());//limit
        listVo.setPage(pageInfo.getPageNum());//page
        listVo.setPages(pageInfo.getPages());//pages
        listVo.setTotal((int) pageInfo.getTotal());//total
        return listVo;
    }

    /**
     * 增加管理员
     *
     * @param Admin avatar图片路径
     *              password管理员密码
     *              roleIds管理员权限
     *              username管理员用户名
     * @return
     */
    @Override
    public BaseRespVo create(MarketAdmin Admin) {
        BaseRespVo baseRespVo = new BaseRespVo();
        //对用户名进行判断
        MarketAdmin username = adminMapper.selectUsername(Admin.getUsername());
        if (username != null) {
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员已经存在");
            return baseRespVo;
        }
        //对密码进行判断
        if (Admin.getPassword().length() < 6) {
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员密码长度不能小于6");
            return baseRespVo;
        }

        MarketAdmin marketAdmin = new MarketAdmin(null, Admin.getUsername(), Admin.getPassword(), null, null, Admin.getAvatar(), new Date(), new Date(), false, Admin.getRoleIds());
        int affectedRows = adminMapper.insert(marketAdmin);
        if (affectedRows != 1) {

        }
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketAdmin);
        return baseRespVo;
    }

    @Override
    public BaseRespVo update(MarketAdmin Admin) {
        BaseRespVo baseRespVo = new BaseRespVo();
        //对用户名进行判断
        MarketAdmin username = adminMapper.selectUsername(Admin.getUsername());
        if (username != null) {
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员已经存在");
            return baseRespVo;
        }
        //对密码进行判断
        if (Admin.getPassword().length() < 6) {
            baseRespVo.setErrno(602);
            baseRespVo.setErrmsg("管理员密码长度不能小于6");
            return baseRespVo;
        }
        MarketAdmin marketAdmin = new MarketAdmin(Admin.getId(), Admin.getUsername(), Admin.getPassword(), null, null, Admin.getAvatar(), new Date(), new Date(), true, Admin.getRoleIds());
        adminMapper.update(marketAdmin);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(marketAdmin);
        return baseRespVo;
    }

    @Override
    public void delete(Integer id) {
        adminMapper.deleteByPrimaryKeyLogically(id);
    }
}
